package com.supreme.cacheserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping(value = "redis-hash")
public class RedisHashController {

    @Resource
    private RedisTemplate<String, Serializable> redisTemplate;

    @Resource
    private List<RedisTemplate<String, Serializable>> redisTemplateList;

    @GetMapping(value = "put/{key}/{hashKey}/{value}")
    public AjaxResp put(@PathVariable(value = "key") String key,
                            @PathVariable(value = "hashKey") String hashKey,
                            @PathVariable(value = "value") String value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
        return AjaxResp.instance();
    }

    @GetMapping(value = "get/{key}/{hashKey}")
    public AjaxResp get(@PathVariable(value = "key") String key, @PathVariable(value = "hashKey") String hashKey) {
        Object value = redisTemplate.opsForHash().get(key, hashKey);
        return AjaxResp.instance(value);
    }

    @GetMapping(value = "multiGet/{key}/{hashKey}")
    public AjaxResp multiGet(@PathVariable(value = "key") String key, @PathVariable(value = "hashKey") String... hashKey) {
        List<Object> objectList = redisTemplate.opsForHash().multiGet(key, Collections.singleton(hashKey));
        return AjaxResp.instance(objectList);
    }

    @GetMapping(value = "entries/{key}")
    public AjaxResp entries(@PathVariable(value = "key") String key) {
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
        return AjaxResp.instance(entries);
    }

    @GetMapping(value = "hasKey/{key}/{hashKey}")
    public AjaxResp hasKey(@PathVariable(value = "key") String key, @PathVariable(value = "hashKey") Object hashKey) {
        Boolean hasKey = redisTemplate.opsForHash().hasKey(key, hashKey);
        return AjaxResp.instance(hasKey);
    }

    @GetMapping(value = "putIfAbsent/{key}/{hashKey}/{value}")
    public AjaxResp putIfAbsent(@PathVariable(value = "key") String key,
                           @PathVariable(value = "hashKey") String hashKey,
                           @PathVariable(value = "value") String value) {
        Boolean putIfAbsent = redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
        return AjaxResp.instance(putIfAbsent);
    }

    @GetMapping(value = "delete/{key}/{hashKey}")
    public AjaxResp delete(@PathVariable(value = "key") String key,
                           @PathVariable(value = "hashKey") Object... hashKey) {
        Long delete = redisTemplate.opsForHash().delete(key, hashKey);
        return AjaxResp.instance(delete);
    }

    @GetMapping(value = "keys/{key}")
    public AjaxResp keys(@PathVariable(value = "key") String key) {
        Set<Object> keys = redisTemplate.opsForHash().keys(key);
        return AjaxResp.instance(keys);
    }

    @GetMapping(value = "values/{key}")
    public AjaxResp values(@PathVariable(value = "key") String key) {
        List<Object> values = redisTemplate.opsForHash().values(key);
        return AjaxResp.instance(values);
    }

    @GetMapping(value = "size/{key}")
    public AjaxResp size(@PathVariable(value = "key") String key) {
        Long size = redisTemplate.opsForHash().size(key);
        return AjaxResp.instance(size);
    }
}
